{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "\n",
    "\n",
    "* HTTP 方法：POST\n",
    "> 请求URL： https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\n",
    "\n",
    "* 请求参数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和 url/pdf_file 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码后进行urlencode，要求base64编码和urlencode...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和 image/pdf_file 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整url，url长度不超过1024字节，url对应的图片base64编码后大小不超过1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>pdf_file</td>\n",
       "      <td>和 image/url 三选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>PDF文件，base64编码后进行urlencode，要求base64编码和urlencod...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>pdf_file_num</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>需要识别的PDF文件的对应页码，当 pdf_file 参数有效时，识别传入页码的对应页面内容...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>language_type</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>auto_detect CHN_ENGENGJAPKORFRESPAPORGERITARUS...</td>\n",
       "      <td>识别语言类型，默认为CHN_ENG可选值包括：- auto_detect：自动检测语言，并识...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>detect_direction</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否检测图像朝向，默认不检测，即：false。朝向是指输入图像是正常方向、逆时针旋转90/1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>paragraph</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否输出段落信息</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>probability</td>\n",
       "      <td>否</td>\n",
       "      <td>string</td>\n",
       "      <td>true/false</td>\n",
       "      <td>是否返回识别结果中每一行的置信度</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 参数                  是否必选      类型  \\\n",
       "0             image    和 url/pdf_file 三选一  string   \n",
       "1               url  和 image/pdf_file 三选一  string   \n",
       "2          pdf_file       和 image/url 三选一  string   \n",
       "3      pdf_file_num                     否  string   \n",
       "4     language_type                     否  string   \n",
       "5  detect_direction                     否  string   \n",
       "6         paragraph                     否  string   \n",
       "7       probability                     否  string   \n",
       "\n",
       "                                               可选值范围  \\\n",
       "0                                                  -   \n",
       "1                                                  -   \n",
       "2                                                  -   \n",
       "3                                                  -   \n",
       "4  auto_detect CHN_ENGENGJAPKORFRESPAPORGERITARUS...   \n",
       "5                                         true/false   \n",
       "6                                         true/false   \n",
       "7                                         true/false   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码后进行urlencode，要求base64编码和urlencode...  \n",
       "1  图片完整url，url长度不超过1024字节，url对应的图片base64编码后大小不超过1...  \n",
       "2  PDF文件，base64编码后进行urlencode，要求base64编码和urlencod...  \n",
       "3  需要识别的PDF文件的对应页码，当 pdf_file 参数有效时，识别传入页码的对应页面内容...  \n",
       "4  识别语言类型，默认为CHN_ENG可选值包括：- auto_detect：自动检测语言，并识...  \n",
       "5  是否检测图像朝向，默认不检测，即：false。朝向是指输入图像是正常方向、逆时针旋转90/1...  \n",
       "6                                           是否输出段落信息  \n",
       "7                                   是否返回识别结果中每一行的置信度  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html('https://ai.baidu.com/ai-doc/OCR/1k3h7y3db')[2]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('[本地文件]', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '[调用鉴权接口获取的token]'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}